"""
数据源模型定义
"""
from django.db import models
from django.utils.translation import gettext_lazy as _


class StockBasic(models.Model):
    """股票基本信息"""
    ts_code = models.CharField(_('股票代码'), max_length=10, unique=True)
    symbol = models.CharField(_('股票代码'), max_length=10)
    name = models.CharField(_('股票名称'), max_length=50)
    area = models.CharField(_('地区'), max_length=50, null=True, blank=True)
    industry = models.CharField(_('行业'), max_length=50, null=True, blank=True)
    market = models.CharField(_('市场类型'), max_length=50, null=True, blank=True)
    list_date = models.DateField(_('上市日期'), null=True, blank=True)
    is_hs = models.CharField(_('是否沪深港通'), max_length=10, null=True, blank=True)
    
    class Meta:
        verbose_name = _('股票基本信息')
        verbose_name_plural = _('股票基本信息')
        ordering = ['ts_code']
        indexes = [
            models.Index(fields=['ts_code']),
            models.Index(fields=['symbol']),
            models.Index(fields=['name']),
        ]
    
    def __str__(self):
        return f"{self.ts_code} - {self.name}"


class StockDaily(models.Model):
    """股票日线行情"""
    stock = models.ForeignKey(StockBasic, on_delete=models.CASCADE, related_name='daily_data', verbose_name=_('股票'))
    trade_date = models.DateField(_('交易日期'))
    open = models.DecimalField(_('开盘价'), max_digits=12, decimal_places=4)
    high = models.DecimalField(_('最高价'), max_digits=12, decimal_places=4)
    low = models.DecimalField(_('最低价'), max_digits=12, decimal_places=4)
    close = models.DecimalField(_('收盘价'), max_digits=12, decimal_places=4)
    pre_close = models.DecimalField(_('昨收价'), max_digits=12, decimal_places=4, null=True, blank=True)
    change = models.DecimalField(_('涨跌额'), max_digits=12, decimal_places=4, null=True, blank=True)
    pct_chg = models.DecimalField(_('涨跌幅'), max_digits=12, decimal_places=4, null=True, blank=True)
    vol = models.DecimalField(_('成交量(手)'), max_digits=20, decimal_places=4, null=True, blank=True)
    amount = models.DecimalField(_('成交额(千元)'), max_digits=20, decimal_places=4, null=True, blank=True)
    
    class Meta:
        verbose_name = _('股票日线行情')
        verbose_name_plural = _('股票日线行情')
        unique_together = ('stock', 'trade_date')
        ordering = ['stock', '-trade_date']
        indexes = [
            models.Index(fields=['stock', 'trade_date']),
            models.Index(fields=['trade_date']),
        ]
    
    def __str__(self):
        return f"{self.stock.ts_code} - {self.trade_date} - {self.close}"


class FinancialIndicator(models.Model):
    """财务指标"""
    stock = models.ForeignKey(StockBasic, on_delete=models.CASCADE, related_name='financial_indicators', verbose_name=_('股票'))
    end_date = models.DateField(_('报告期'))
    eps = models.DecimalField(_('每股收益'), max_digits=12, decimal_places=4, null=True, blank=True)
    dt_eps = models.DecimalField(_('每股净利润'), max_digits=12, decimal_places=4, null=True, blank=True)
    total_revenue_ps = models.DecimalField(_('每股营业总收入'), max_digits=12, decimal_places=4, null=True, blank=True)
    revenue_ps = models.DecimalField(_('每股营业收入'), max_digits=12, decimal_places=4, null=True, blank=True)
    bps = models.DecimalField(_('每股净资产'), max_digits=12, decimal_places=4, null=True, blank=True)
    roe = models.DecimalField(_('净资产收益率'), max_digits=12, decimal_places=4, null=True, blank=True)
    profit_to_gr = models.DecimalField(_('净利润/营业总收入'), max_digits=12, decimal_places=4, null=True, blank=True)
    op_to_gr = models.DecimalField(_('营业利润/营业总收入'), max_digits=12, decimal_places=4, null=True, blank=True)
    debt_to_assets = models.DecimalField(_('资产负债率'), max_digits=12, decimal_places=4, null=True, blank=True)
    
    class Meta:
        verbose_name = _('财务指标')
        verbose_name_plural = _('财务指标')
        unique_together = ('stock', 'end_date')
        ordering = ['stock', '-end_date']
        indexes = [
            models.Index(fields=['stock', 'end_date']),
            models.Index(fields=['end_date']),
        ]
    
    def __str__(self):
        return f"{self.stock.ts_code} - {self.end_date}"
